//
//  CurrentUser.m
//  SportSocial
//
//  Created by yull on 14/12/21.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "CurrentUser.h"
#import "Message.pb.h"

@implementation CurrentUser

+ (CurrentUser *)shareInstance
{
    static CurrentUser *currentUser = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        currentUser = [[CurrentUser alloc] init];
    });
    return currentUser;
}

- (void)updateWithDict:(NSDictionary *)dict
{
    for (NSString *key in dict.allKeys) {
        id value = [dict objectForKey:key];
        if ([value isKindOfClass:[NSNull class]]) {
            value = @"";
        }
        
        [self setValue:value forKey:key];
    }
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    [super setValue:value forUndefinedKey:key];
    
}

- (BOOL)isMyFriend:(UInt64)friendID
{
    for (FriendInfo *friendInfo in self.friendList) {
        if (friendInfo.userId == friendID) {
            return YES;
        }
    }
    
    return NO;
}

- (BOOL)isMyGroup:(UInt64)groupID
{
    for (GroupInfo *groupInfo in self.groupList) {
        if (groupInfo.groupId == groupID) {
            return YES;
        }
    }
    
    return NO;
}

- (void)addGroup:(GroupInfo *)groupInfo
{
    if (!self.groupList) {
        self.groupList = [NSMutableArray array];
    }
    
    [self.groupList addObject:groupInfo];
}

- (void)deleteGroup:(UInt64)groupID
{
    for (GroupInfo *groupInfo in self.groupList) {
        if (groupInfo.groupId == groupID) {
            [self.groupList removeObject:groupInfo];
            break;
        }
    }
}

- (NSString *)deviceToken
{
    return _deviceToken.length ? _deviceToken : @"";
}

- (BOOL)isGroup:(UInt64)groupID containUser:(UInt64)friendID
{
    GroupInfo *groupInfo = nil;
    for (GroupInfo *info in self.groupList) {
        if (info.groupId == groupID) {
            groupInfo = info;
            break;
        }
    }
    
    if (!groupInfo) {
        return NO;
    }
    
    NSArray *array = [groupInfo userListList];
    
    for (GroupUserInfo *userInfo in array) {
        if (userInfo.userId == friendID) {
            return YES;
        }
    }
    
    return NO;
}

@end
